using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace Tasks
{
    public abstract class Tasks : IEnumerable
    {
        public Tasks()
        {
            this.registeredEnumerators = new List<IEnumerator>();
        }

        protected List<IEnumerator> registeredEnumerators { get; private set; }

        public bool isRunning { get; protected set; }

        public int registeredTasks
        {
            get
            {
                return this.registeredEnumerators.Count;
            }
        }

        public void Add(ITask task)
        {
            if (task == null)
            {
                throw new ArgumentNullException();
            }
            this.Add(new EnumerableTask(task));
        }

        public void Add(IEnumerable enumerable)
        {
            if (enumerable is Tasks && (enumerable as Tasks).registeredTasks == 0)
            {
                UnityEngine.Debug.LogError("Avoid to Register zero size tasks");
            }
            if (enumerable == null)
            {
                throw new ArgumentNullException();
            }
            this.registeredEnumerators.Add(enumerable.GetEnumerator());
        }

        public void Add(IEnumerator enumerator)
        {
            if (enumerator == null)
            {
                throw new ArgumentNullException();
            }
            this.registeredEnumerators.Add(enumerator);
        }

        public abstract IEnumerator GetEnumerator();
    }
}
